package org.xml;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class XmlTest {

    public static String wmuEduTicketAuthorize(String service,String ticket) {
        String url = "https://authserver.wmu.edu.cn/authserver/serviceValidate";
        url+="?service="+service+"&ticket="+ticket;
        try (HttpResponse response= HttpRequest.get(url).timeout(5000).execute()){
            String body = response.body();

        }
        return null;
    }

    public static void main(String[] args) {

        String xml = "<cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'> \n" +
                "<cas:authenticationSuccess> \n" +
                "<cas:user>username</cas:user> \n" +
                "<cas:name>张三</cas:name> \n" +
                "<cas:employeeNumber>12345</cas:employeeNumber> \n" +
                "</cas:authenticationSuccess> \n" +
                "</cas:serviceResponse>";

        xml = "<cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'>\n" +
                "    <cas:authenticationFailure code=\"INVALID_TICKET\">Ticket &#39;ST-123456&#39; not recognized</cas:authenticationFailure>\n" +
                "</cas:serviceResponse>\n";

        xml = "<cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'>\n" +
                "    <cas:authenticationSuccess>\n" +
                "        <cas:user>3ATEST9999</cas:user>\n" +
                "        <cas:attributes>\n" +
                "            <cas:name>测试</cas:name>\n" +
                "            <cas:employeeNumber>3ATEST9999</cas:employeeNumber>\n" +
                "        </cas:attributes>\n" +
                "    </cas:authenticationSuccess>\n" +
                "</cas:serviceResponse>";


        //1.创建Reader对象
        SAXReader reader = new SAXReader();
        //2.加载xml
        Document document = null;
        try {
            document = reader.read(new StringReader(xml));
        } catch (DocumentException e) {
            throw new RuntimeException(e);
        }
        Map<String,String> data = new HashMap<>();
        //3.获取根节点
        Element rootElement = document.getRootElement();
        Element successElement = rootElement.element("authenticationSuccess");
        if (Objects.nonNull(successElement)){
            Element user = successElement.element("user");
            Element name = successElement.element("name");
            Element employeeNumber = successElement.element("employeeNumber");
//            System.out.println("user="+user.getStringValue());
//            System.out.println("name="+name.getStringValue());
//            System.out.println("employeeNumber="+employeeNumber.getStringValue());
            Element attributes = successElement.element("attributes");
            if (Objects.isNull(employeeNumber) && Objects.nonNull(attributes)){
                employeeNumber = attributes.element("employeeNumber");
            }
            if (Objects.nonNull(employeeNumber)){
               String idCard = employeeNumber.getStringValue();
                System.out.println("employeeNumber="+idCard);
            }
        }
        Element failureElement = rootElement.element("authenticationFailure");
        if (Objects.nonNull(failureElement)){
            System.out.println("failure="+failureElement.getStringValue());
        }
    }
}
